package net.xt.testthread.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

/**
 * 自定义函数式接口
 */
public class CustomFun {

    /**
     * 对list集合的操作
     *
     * @param srcList  原list集合
     * @param function 对list集合具体操作的方法
     * @return 新list集合
     */
    public List<?> exeList(List<?> srcList,
                           Function<List<?>, List<?>> function) {
        List<?> targetList = function.apply(srcList);
        return targetList;
    }

    public List<?> listAdd(List<?> srcList) {
        List<Object> reslutList = new ArrayList<>();
        reslutList.addAll(srcList);
        reslutList.add(999);

        return reslutList;
    }

    public List<?> listDel(List<?> srcList) {
        List<Object> reslutList = new ArrayList<>();
        reslutList.addAll(srcList);
        reslutList.remove(0);

        return reslutList;
    }

    public List<?> listSort(List<?> srcList) {
        List<Object> reslutList = new ArrayList<>();
        reslutList.addAll(srcList);
        reslutList.sort((o1, o2) -> {
            Integer o11 = (Integer) o1;
            Integer o22 = (Integer) o2;
            return o22 - o11;
        });

        return reslutList;
    }

    public static void main(String[] args) {
        CustomFun customerFun = new CustomFun();
        List<Integer> srcList = Arrays.asList(12, 34, 45, 56, 11);

        List<Integer> resultList = (List<Integer>) customerFun.exeList(srcList, customerFun::listAdd);
        System.out.println("resultList = " + resultList);

        List<Integer> resultList2 = (List<Integer>) customerFun.exeList(srcList, customerFun::listDel);
        System.out.println("resultList2 = " + resultList2);

        List<Integer> resultList3 = (List<Integer>) customerFun.exeList(srcList, customerFun::listSort);
        System.out.println("resultList3 = " + resultList3);
    }

}
